package com.lancoo.znbkxx.teacher.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.*
import com.lancoo.znbkxx.teacher.model.data.PaperCatlog
import com.lancoo.znbkxx.teacher.model.data.PaperLibraryCatalog
import kotlinx.android.synthetic.main.view_filter_school.view.*

/**
 *@author Admin
 *@date 2021/2/4
 *
 */
class SchoolPopupWindow(context: Context, onClickListener: View.OnClickListener,
                        val listener: OnSelectedListener,
                        val data: List<PaperCatlog>) :
        PopupWindow(context) {
    private var adapter: PaperCatlogAdapter
    private lateinit var adapter2nd: PaperCatlogAdapter2nd
    private lateinit var adapter3rd: PaperCatlogAdapter3rd
    private lateinit var adapter4th: PaperCatlogAdapter4th
    var paperType = ""

    init {
        val contentView = LayoutInflater.from(context).inflate(R.layout.view_filter_school, null)
        adapter = PaperCatlogAdapter(data)
        adapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
            resetValue()
            data[position].isSelected = true
            paperType = data[position].PaperType
            adapter.notifyDataSetChanged()
            listener.onSelect(0, data[position].OneName)
            val data2nd = data[position].TwoList
            if (data2nd.isNotEmpty()) {
                contentView.rv_b.visibility = View.VISIBLE
                contentView.rv_c.visibility = View.INVISIBLE
                contentView.rv_d.visibility = View.INVISIBLE
                adapter2nd = PaperCatlogAdapter2nd(data2nd)
                contentView.rv_b.layoutManager = LinearLayoutManager(context)
                adapter2nd.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
                    data2nd.forEach {
                        it.isSelected = false
                    }
                    data2nd[position].isSelected = true
                    paperType = data2nd[position].PaperType
                    adapter2nd.notifyDataSetChanged()
                    listener.onSelect(1, data2nd[position].TwoName)
                    val data3rd = data2nd[position].ThreeList
                    if (data3rd.isNotEmpty()) {
                        contentView.rv_c.visibility = View.VISIBLE
                        contentView.rv_d.visibility = View.INVISIBLE
                        adapter3rd = PaperCatlogAdapter3rd(data3rd)
                        contentView.rv_c.layoutManager = LinearLayoutManager(context)
                        adapter3rd.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
                            data3rd.forEach {
                                it.isSelected = false
                            }
                            data3rd[position].isSelected = true
                            paperType = if (!data3rd[position].PaperType.contains("教材版本")) {
                                data3rd[position].PaperType
                            } else {
                                ""
                            }
                            adapter3rd.notifyDataSetChanged()
                            listener.onSelect(2, data3rd[position].ThreeName)
                            val data4th = data3rd[position].FourList
                            if (data4th.isNotEmpty()) {
                                contentView.rv_d.visibility = View.VISIBLE
                                adapter4th = PaperCatlogAdapter4th(data4th)
                                contentView.rv_d.layoutManager = LinearLayoutManager(context)
                                adapter4th.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
                                    data4th.forEach {
                                        it.isSelected = false
                                    }
                                    data4th[position].isSelected = true
                                    paperType = data4th[position].PaperType
                                    adapter4th.notifyDataSetChanged()
                                    listener.onSelect(3, data4th[position].FourName)
                                }
                                contentView.rv_d.adapter = adapter4th
                            } else {
                                contentView.rv_d.visibility = View.INVISIBLE
                            }
                        }
                        contentView.rv_c.adapter = adapter3rd
                    } else {
                        contentView.rv_c.visibility = View.INVISIBLE
                        contentView.rv_d.visibility = View.INVISIBLE
                    }
                }
                contentView.rv_b.adapter = adapter2nd
            } else {
                contentView.rv_b.visibility = View.INVISIBLE
                contentView.rv_c.visibility = View.INVISIBLE
                contentView.rv_d.visibility = View.INVISIBLE
            }
        }
        contentView.rv_a.layoutManager = LinearLayoutManager(context)
        contentView.rv_a.adapter = adapter
        contentView.tv_reset.setOnClickListener(onClickListener)
        contentView.tv_done.setOnClickListener(onClickListener)
        this.contentView = contentView
        this.width = WindowManager.LayoutParams.MATCH_PARENT
        this.height = WindowManager.LayoutParams.WRAP_CONTENT
        this.isOutsideTouchable = false
        this.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        this.isFocusable = true
        update()
    }

    fun resetValue() {
        data.forEach {
            it.isSelected = false
            it.TwoList.forEach { two ->
                two.isSelected = false
                two.ThreeList.forEach { three ->
                    three.isSelected = false
                    three.FourList.forEach { four ->
                        four.isSelected = false
                    }
                }
            }
        }
    }

    interface OnSelectedListener {
        fun onSelect(position: Int, text: String)
    }
}